<?php

namespace App\Http\Controllers\Backend;

use App\Http\Requests\Company\StyleRequest;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Services\Business\Models\style;


class StyleController extends BaseController
{


    public function index(Request $request)
    {


        $companyId = $this->getCompanyId($request->input('app_id'));
        $styles = Style::query()
            ->where('company_id',$companyId)
            ->orderByDesc('created_at')
            ->paginate($request->input('size', 10));
        return $this->successData(compact('styles'));
    }


    /**
     * @param StyleRequest $request
     * @param style $style
     * @return JsonResponse
     */
    public function store(StyleRequest $request, Style $style)
    {

        $data = $request->filldata();
        $style->fill($data);
        $style->save();
        return $this->success(__('创建风格成功'));
    }


    /**
     * 风格编辑
     * @param $id
     * @return JsonResponse
     */
    public function edit($id)
    {

        $style = Style::query()->where('id',$id)->first();
        return $this->successData(compact('style'));
    }




    /**
     * 风格更新
     * @param StyleRequest $request
     * @param $id
     * @return JsonResponse
     */
    public function update(StyleRequest $request, $id)
    {

        $style = Style::query()->where('id',$id)->first();
        $data = $request->filldata();
        $style->fill($data);
        $style->save();

        return $this->success(__('风格更新成功'));
    }


    /**
     * 风格删除
     * @param $id
     * @return JsonResponse
     */
    public function delete($id)
    {


        $style =  Style::query()->where('id',$id)->first();
        if ($style->customers()->exists()) {
            return  $this->error(__('不能删除'));
        }
        $style->delete();
        return $this->success(__('删除成功'));
    }








}